package com.hzjy.manage.controller;

import com.hzjy.common.core.controller.BaseController;
import com.hzjy.common.core.domain.AjaxResult;
import com.hzjy.common.core.domain.model.LoginUser;
import com.hzjy.common.core.page.TableDataInfo;
import com.hzjy.common.utils.ServletUtils;
import com.hzjy.framework.web.service.TokenService;
import com.hzjy.manage.service.impl.AuditDataFinalServiceImpl;
import com.hzjy.manage.service.impl.AuditDataFirstServiceImpl;
import com.hzjy.manage.service.impl.AuditRecordServiceImpl;
import com.hzjy.manage.vo.AuditRecord;
import com.hzjy.manage.vo.HzjyProduct;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 挂牌资料提交审核-终审
 */
@Api("终审")
@RestController
@RequestMapping("/manage/audit_data_final")
public class AuditDataFinalController extends BaseController {
    @Autowired
    private TokenService tokenService;
    @Autowired
    private AuditDataFinalServiceImpl auditDataFinalService;
    @Autowired
    private AuditDataFirstServiceImpl auditDataFirstService;


    @ApiOperation("获取初审通过的集合")
    @PreAuthorize("@ss.hasPermi('manage:audit_data_final:list')")
    @PostMapping("/list")
    public TableDataInfo list(HzjyProduct hzjyProduct){
        startPage(); //设置为需要分页
        List<HzjyProduct> list = auditDataFinalService.selectlist(hzjyProduct);
        return getDataTable(list);
    }

    @ApiOperation("查看详情")
    @PreAuthorize("@ss.hasPermi('manage:audit_data_final:select')")
    @PostMapping("/get_audit_data")
    public AjaxResult getAuditData(@RequestBody HzjyProduct hzjyProduct){
        return AjaxResult.success(auditDataFirstService.selectByIdInfo(hzjyProduct.getProductId()));
    }


    @ApiOperation("终审通过")
    @PreAuthorize("@ss.hasPermi('manage:audit_data_final:agree')")
    @PostMapping("/agree_audit_data")
    public AjaxResult agreeAuditData(@RequestBody AuditRecord auditRecord){
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        HzjyProduct hzjyProduct =auditDataFinalService.selectById(auditRecord.getProductId());
        auditRecord.setUserId(loginUser.getUser().getUserId());
        auditRecord.setFirstState(hzjyProduct.getState());
        auditRecord.setProductId(hzjyProduct.getProductId());
        auditDataFinalService.agreeAuditData(auditRecord);

        //查询修改后的状态
        HzjyProduct newHzjyProduct = auditDataFirstService.selectById(auditRecord.getProductId());
        //插入消息通知
        auditDataFirstService.insertTradeAuditNotice(newHzjyProduct);
        return AjaxResult.success();
    }

    @ApiOperation("终审驳回")
    @PreAuthorize("@ss.hasPermi('manage:audit_data_final:refuse')")
    @PostMapping("/refuse_audit_data")
    public AjaxResult refuseAuditData(@RequestBody AuditRecord auditRecord){
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        HzjyProduct hzjyProductNow =auditDataFinalService.selectById(auditRecord.getProductId());
        auditRecord.setUserId(loginUser.getUser().getUserId());
        auditRecord.setFirstState(hzjyProductNow.getState());
        auditDataFinalService.refuseAuditData(auditRecord);

        //查询修改后的状态
        HzjyProduct newHzjyProduct = auditDataFirstService.selectById(auditRecord.getProductId());
        //插入消息通知
        auditDataFirstService.insertTradeAuditNotice(newHzjyProduct);
        return AjaxResult.success();
    }

}
